package com.cmd.project.controller;

import com.cmd.common.annotation.Anonymous;
import com.cmd.common.annotation.Log;
import com.cmd.common.core.controller.BaseController;
import com.cmd.common.core.domain.AjaxResult;
import com.cmd.common.core.page.TableDataInfo;
import com.cmd.common.enums.BusinessType;
import com.cmd.common.utils.SecurityUtils;
import com.cmd.common.utils.poi.ExcelUtil;
import com.cmd.project.domain.StudentRegistration;
import com.cmd.project.service.IStudentRegistrationService;
import com.cmd.project.vo.StudentRegistrationVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 学生报名信息Controller
 *
 * @author cq
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/project/registration")
public class StudentRegistrationController extends BaseController {
    @Autowired
    private IStudentRegistrationService studentRegistrationService;

    /**
     * 查询学生报名信息列表
     */
    @PreAuthorize("@ss.hasPermi('project:registration:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentRegistration studentRegistration) {
        startPage();
        List<StudentRegistration> list = studentRegistrationService.selectStudentRegistrationList(studentRegistration);
        return getDataTable(list);
    }



    /**
     * 导出学生报名信息列表
     */
    @PreAuthorize("@ss.hasPermi('project:registration:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentRegistration studentRegistration) {
        List<StudentRegistration> list = studentRegistrationService.selectStudentRegistrationList(studentRegistration);
        list.forEach(item -> {
            String thumbnailUrlStr = item.getThumbnail();
            List<String> urls = Arrays.stream(thumbnailUrlStr.split(","))
                    .filter(s -> StringUtils.isNotBlank(s))
                    .map(e->("http://activity.hsmy.art/ssAdmin-api"+e).trim())
                    .collect(Collectors.toList());
            item.setThumbnailUrl(StringUtils.join(urls,"\n"));

        });
        ExcelUtil<StudentRegistration> util = new ExcelUtil<StudentRegistration>(StudentRegistration.class);
        util.exportExcel(response, list, "学生报名信息数据");
    }

    /**
     * 获取学生报名信息详细信息
     */
    @GetMapping(value = "/{id}")
    @Anonymous
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(studentRegistrationService.selectStudentRegistrationById(id));
    }

    /**
     * 新增学生报名信息
     */
    @Anonymous
    @PostMapping
    public AjaxResult add(@RequestBody StudentRegistration studentRegistration) {
        return toAjax(studentRegistrationService.insertStudentRegistration(studentRegistration));
    }

    /**
     * 修改学生报名信息
     */
    @PutMapping
    @Anonymous
    public AjaxResult edit(@RequestBody StudentRegistration studentRegistration) {
        return toAjax(studentRegistrationService.updateStudentRegistration(studentRegistration));
    }

    /**
     * 删除学生报名信息
     */
    @PreAuthorize("@ss.hasPermi('project:registration:remove')")
    @Log(title = "学生报名信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(studentRegistrationService.deleteStudentRegistrationByIds(ids));
    }

    @GetMapping("/listByH5")
    @Anonymous
    public AjaxResult listByIdNumber(StudentRegistration studentRegistration) {
//        try {
//            studentRegistration.setCreateBy(SecurityUtils.getUsername());
//        } catch (Exception e) {
//        }
        List<StudentRegistration> list = studentRegistrationService.selectStudentRegistrationList(studentRegistration);
        return AjaxResult.success(list);
    }

    /**
     * 下载导入模板
     */
    @PreAuthorize("@ss.hasPermi('project:registration:export')")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<StudentRegistrationVo> util = new ExcelUtil<StudentRegistrationVo>(StudentRegistrationVo.class);
        util.importTemplateExcel(response, "学生报名信息数据");
    }

    /**
     * 导入学生报名信息数据
     */
    @PreAuthorize("@ss.hasPermi('project:registration:export')")
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file) throws Exception {
        ExcelUtil<StudentRegistrationVo> util = new ExcelUtil<StudentRegistrationVo>(StudentRegistrationVo.class);
        List<StudentRegistrationVo> registrationList = util.importExcel(file.getInputStream());
        String message = studentRegistrationService.importStudentRegistration(registrationList);
        return success(message);
    }


}
